package com.horoscope.astrology.zodiac.signs.free.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.ArrayMap
import android.widget.TextView
import androidx.viewpager.widget.ViewPager
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.adapter.quize.QuizPagerAdapter
import com.horoscope.astrology.zodiac.signs.free.bean.QuizBean
import com.horoscope.astrology.zodiac.signs.free.view.NoScrollViewPager
import kotlinx.android.synthetic.main.activity_quiz.*

class QuizActivity : Base2Activity() {

    private lateinit var mViewPager: NoScrollViewPager
    private var title: TextView? = null
    private var mBean = QuizBean()
    private var questionSize: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        immersionWhite()
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_quiz)
        title = findViewById(R.id.text_view_back_title)
        mViewPager = findViewById(R.id.view_pager_quiz)
        intent.getSerializableExtra("bean")?.let {
            mBean = it as QuizBean
            questionSize = mBean.questions.size
            title!!.text = StringBuilder().append("1/").append(questionSize)
            mViewPager.adapter = QuizPagerAdapter(supportFragmentManager, mBean)
        }
        mViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                title!!.text = StringBuilder("${position + 1}/${questionSize}")
            }

        })
        mViewPager.setNoScroll(true)
        btn_back?.setOnClickListener {
            finish()
        }
    }

    // 判断是否答完所有的题目
    fun setPage() {
        val currentPage = mViewPager.currentItem
        if (currentPage + 1 < mViewPager.childCount) {
            mViewPager.postDelayed({
                mViewPager.currentItem = currentPage + 1
            }, 500)
        }
        if (currentPage + 1 == mViewPager.childCount) {
            startActivity(ResultActivity.generateIntent(this, mBean, getMaxSelect().toInt()))
            finish()
        }

    }

    private val map = ArrayMap<String, Int>()
    fun setAnswer(key: String) {// key = 0,1,2,3  -> a,b,c,d
        if (map[key] == null) {
            map[key] = 1
        } else {
            map[key] = map[key]?.plus(+1)
        }
    }

    // 获得答题结果
    private fun getMaxSelect(): String {
        var res = 0
        var key = "0"
        map.keys.forEach {
            if (map[it]!!.toInt() > res) {
                res = map[it]!!.toInt()
                key = it
            }else{
                key = "0"
            }
        }
        return key
    }


    companion object {
        fun generateIntent(context: Context, bean: QuizBean): Intent {
            val intent = Intent(context, QuizActivity::class.java)
            intent.putExtra("bean", bean)
            return intent
        }
    }
}
